package com.botao.tip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;;
import com.botao.tip.entity.Product;
import com.botao.tip.service.ProductService;
import com.botao.tip.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : vip865047755@126.com
 * @version :JDK1.8
 * @date : 2020-12-03 16:47
 * @File : ProductController.java
 * @software: IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/product/")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("queryAll")
    public AjaxResult queryAllProducts(@RequestParam int currentPage, @RequestParam int pageSize) {
        Page<Product> page = productService.page(new Page<>(currentPage, pageSize));
        List<Product> records = page.getRecords();

        long total = page.getTotal();
        long current = page.getCurrent();
        long size = page.getSize();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();

        HashMap<Object, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        map.put("current", current);
        map.put("size", size);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return AjaxResult.success(map);
    }

    @GetMapping("get/{id}")
    public AjaxResult getOne(@PathVariable String id) {
        Product product = productService.getOne(new QueryWrapper<Product>().eq("id", id));
        if (product == null) {
            return AjaxResult.error();
        }
        return AjaxResult.success(product);
    }


    @DeleteMapping("delete/{id}")
    public AjaxResult delete(@PathVariable String id) {
        return productService.removeById(id) ? AjaxResult.success() : AjaxResult.error();
    }

    @RequestMapping("update")
    public AjaxResult update(@RequestParam("id") String id,
                             @RequestParam("product") String product,
                             @RequestParam("manufacturer") String manufacturer,
                             @RequestParam("inventory") String inventory,
                             @RequestParam("state") String state,
                             @RequestParam("money") String money
    ) {
        boolean flag = productService.updateProduct(id, product, manufacturer, inventory, state, money);
        return flag ? AjaxResult.success() : AjaxResult.error(500, "更新失败");
    }

    @RequestMapping("insert")
    public AjaxResult insert(@RequestParam("product") String product,
                             @RequestParam("manufacturer") String manufacturer,
                             @RequestParam("inventory") String inventory,
                             @RequestParam("state") String state,
                             @RequestParam("money") String money
    ) {
        Boolean insert = productService.insert(product, manufacturer, inventory, state, money);
        return insert ? AjaxResult.success("新增成功。") : AjaxResult.error(500, "新增失败。");
    }

    @GetMapping("getHadoop")
    public AjaxResult getHadoop() {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<Product>();
        queryWrapper.select("sum(inventory) as sumAll");
        Product all = productService.getOne(queryWrapper);
        if (all == null) {
            return AjaxResult.error("查询为空");
        }
        QueryWrapper<Product> queryWrapper2 = new QueryWrapper<Product>();
        queryWrapper2.select(" inventory,count(1) as counts ");
        queryWrapper2.groupBy("inventory");
        HashMap<Object, Object> map = new HashMap<>();
        List<Product> list = productService.list(queryWrapper2);
        map.put("all", all.getSumAll());
        map.put("list", list);
        return AjaxResult.success(map);
    }

    @GetMapping("getMarketView")
    public AjaxResult getMarketView() {
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Product> ge1 = productQueryWrapper.select("product,inventory").ge("Inventory", 1000).groupBy("inventory");
        List<Map<String, Object>> maps = productService.listMaps(ge1);
        if (maps.isEmpty()) {
            return AjaxResult.error("查询为空。");
        }
        QueryWrapper<Product> productQueryWrapper1 = new QueryWrapper<>();
        QueryWrapper<Product> ge2 = productQueryWrapper1.select("product,inventory").le("Inventory", 50).groupBy("inventory");
        List<Map<String, Object>> maps1 = productService.listMaps(ge2);
        if (maps1.isEmpty()) {
            return AjaxResult.error("查询为空。");
        }

        QueryWrapper<Product> queryWrapper = new QueryWrapper<Product>();
        queryWrapper.select("sum(inventory) as sumAll");
        Product all = productService.getOne(queryWrapper);
        if (all == null) {
            return AjaxResult.error("查询为空");
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("moreK", maps);
        hashMap.put("smallF", maps1);
        hashMap.put("counts", all.getSumAll());
        return AjaxResult.success(hashMap);
    }
}
